package com.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.model.dto.CampusQueryDTO;
import com.book.model.entity.Campus;
import com.book.mapper.CampusMapper;
import com.book.service.CampusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 校区 服务实现类
 * </p>
 *
 * @author auto
 * @since 2025-04-03 10:23:06
 */
@Service
public class CampusServiceImpl extends ServiceImpl<CampusMapper, Campus> implements CampusService {

    @Override
    public List<Campus> getCampusList() {
        return this.list();
    }

    @Override
    public Page<Campus> getCampusListByPage(CampusQueryDTO dto) {
        Page<Campus> page = new Page<>(Long.parseLong(dto.getPage()), Long.parseLong(dto.getSize()));
        LambdaQueryWrapper<Campus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(dto.getCampusName()),Campus::getCampusName, dto.getCampusName());
        return this.page(page,queryWrapper);
    }

    @Override
    public Campus addCampus(Campus campus) {
        if(StringUtils.isNotBlank(campus.getCampusId())){
            campus.setCreateTime(new Date());
        }
        this.saveOrUpdate(campus);
        return campus ;
    }
}
